let zencode = require("./zencode");
var fs = require("fs");
//Roller: node routerGenerator.js -count 10 -dir v -prefix 'media_list_cell_' -u 'listview' -left 49 -top 50 -width 150 -height 200 -br 8
//node routerGenerator.js -count 3 -prefix 'category_list_cell_' -u 'listview.*' -left 59 -top 248 -width 142 -height 30 -y 6
//默认配置
var option = {
    dir : 'h',
    prefix : 'media_list_cell',
    u : false,
    r : false,
    d : false,
    l : false,
    left : 0,
    top : 0,
    width : 100,
    height : 100,
    br : 8,
    count : 5,
    line : 2,
    x : 0,
    y : 0

};
//对命令行参数进行整理
for(var i = 2; i < process.argv.length; i+=2){
    if(!isNaN(process.argv[i+1])){
        process.argv[i+1] = parseInt(process.argv[i+1]);
    }
    option[process.argv[i].substring(1)] = process.argv[i+1];
    
}
//读取配置文件
var data = fs.readFileSync(__dirname+"/src/router.config.json");
var oConfig = JSON.parse(data);
//如果命令参数有module，并在配置文件找到该模块，使用配置文件的配置，否则就是默认配置
if(oConfig[option.module]){
    option = Object.assign(option,oConfig[option.module]);
}
if(!option.zen){
    option.zen = "div#"+option.prefix+"_$."+option.prefix+"_$."+option.prefix;
}
//获取配置数据，开始生成路由
var iCount = option.count,iLine = option.line, strPrefix = option.prefix,strDir = option.dir, strUpMatch = option.u, strRightMatch = option.r, strDownMatch = option.d, strLeftMatch = option.l, iWidth = option.width, iHeight = option.height, iInitLeft = option.left, iInitTop = option.top, iBr = option.br,xS = option.x, yS = option.y;
var iTotal = iCount*iLine;
var str = "";
var strHTML = "";
for(var i = 1; i <= iTotal; i++){
    let o = {};
    //v
    //'category_list_cell_1': { 'index':1, 'path': [false, 'listview.*', 'category_list_cell_2', false], 'transitions': { left: 59, top: 248, width: 142, height: 30, borderRadius: '8px' } },
    //h
    //'news_list_cell_1': { 'index':1, 'path': ['page.*', false, 'news_list_cell_2', 'promotionList.*'], 'transitions': { left: 948, top: 178, width: 266, height: 22, borderRadius: '8px' } },
    var aPath = [], iLeft, iTop;
    if(strDir == "h"){//横向配置
        if(i <= iCount){//上
            aPath[0] = strUpMatch;
        }else{
            aPath[0] = strPrefix + "_" +(i-iCount);
        }
        if(i%(iCount) != 0){//右
            aPath[1] = strPrefix + "_" +(i+1);
        }else{
            aPath[1] = strRightMatch;
        }
        if(i+iCount <= iTotal){//下
            aPath[2] = strPrefix + "_" +(i+iCount);
        }else{
            aPath[2] = strDownMatch;
        }
        if((i-1)%iCount!=0){//左
            aPath[3] = strPrefix + "_" +(i-1);
        }else{
            aPath[3] = strLeftMatch;
        }
        iLeft = iInitLeft + (xS + iWidth)*((i-1)%iCount);
        iTop = iInitTop + (yS + iHeight)*Math.floor(i/(iCount+1));
    }else{//纵向配置
        if(i%iCount == 1){//上
            aPath[0] = strUpMatch;
        }else{
            aPath[0] = strPrefix + "_" +(i-1);
        }
        if(i+iCount <= iTotal){//右
            aPath[1] = strPrefix + "_" +(i+iCount);
        }else{
            aPath[1] = strRightMatch;
        }
        if(i%iCount != 0){//下
            aPath[2] = strPrefix + "_" +(i+1);
        }else{
            aPath[2] = strDownMatch;
        }
        if(i > iCount){//左
            aPath[3] = strPrefix + "_" +(i-iCount);
        }else{
            aPath[3] = strLeftMatch;
        }

        iLeft = iInitLeft + (xS + iWidth)*Math.floor(i/(iCount+1));
        iTop = iInitTop + (yS + iHeight)*((i-1)%iCount);
    }
    //o[strPrefix + "_" +i]
    //替换$符号 比如option.r = "delete_$",则根据行数进行相应的匹配
    aPath = JSON.parse(JSON.stringify(aPath).replace(/\$/g,i));
    o = {
        index : i,
        path : aPath,
        transitions : {
            left : iLeft,
            top : iTop,
            width : iWidth,
            height : iHeight,
            borderRadius : iBr + "px"
        }
    }
    //路由
    str += strPrefix + "_" + i + ":" + JSON.stringify(o) + ",\n";

    //html
    strHTML += zencode.peel(option.zen).replace(/\$/g,i) + "\n";

}
//写入
fs.writeFileSync(__dirname + "/dist/router_"+(option.module||"default")+".txt",str,"UTF-8");
fs.writeFileSync(__dirname + "/dist/html_"+(option.module||"default")+".html",strHTML,"UTF-8");